//
//  AppDelegate.m
//  Big Show
//
//  Created by 赵晓航 on 15-5-13.
//  Copyright (c) 2015年 Big_Q. All rights reserved.
//

#import "AppDelegate.h"
#import "XHFindViewController.h"
#import "XHCollectionViewController.h"
#import "XHEditViewController.h"
#import "XHMoreViewController.h"
#import "MMExampleDrawerVisualStateManager.h"
#import "MMDrawerBaseNavigationViewController.h"
#import "UIViewController+MMDrawerController.h"
#import "WXSanFangViewController.h"
#import "AFNetworkReachabilityManager.h"
#import <ShareSDK/ShareSDK.h>
#import "WeiboApi.h"
#import "WeiboSDK.h"




@interface AppDelegate ()<UITabBarControllerDelegate>

@property (nonatomic, retain) UIButton *btn;



@end

@implementation AppDelegate

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
//    [self createNavi];
    
    
    [self createNotificationCenter];
    
    
    
    
    [ShareSDK registerApp:@"7b964db2497e"];
    //添加新浪微博应用 注册网址 http://open.weibo.com
    [ShareSDK connectSinaWeiboWithAppKey:@"568898243"
                               appSecret:@"38a4f8204cc784f81f9f0daaf31e02e3"
                             redirectUri:@"http://www.sharesdk.cn"];
    
    // 导航栏标题颜色
    UIColor *color = [UIColor whiteColor];
    
    NSDictionary *dict = [NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
    [UINavigationBar appearance].titleTextAttributes = dict;
    // 导航颜色
    [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:229/255.0 green:103/255.0 blue:136/255.0 alpha:1.0]];
    
    
    [[UITabBar appearance] setBarTintColor:[UIColor colorWithRed:229/255.0 green:103/255.0 blue:136/255.0 alpha:0.5]];
    
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    
    UIColor *titleHignlightedColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.00];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:titleHignlightedColor, NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
    
    
   
//     引导页
//    self.isOn = NO;
//    NSString *isHasFiel = [[NSUserDefaults standardUserDefaults] objectForKey:@"isFirstOrNot"];
//    if (!(isHasFiel == nil)) {
//        [self createNavi];
//    }else
//    {
        [self makeLauchView];
//        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"isFirstOrNot"];
//    }

    
    [_window release];
    
    return YES;
}

//引导页
-(void)makeLauchView{
    NSArray *arr = [NSArray arrayWithObjects:@"yindao1.jpg",@"yindao2.jpg",@"yindao3.jpg", nil];
    
    self.scr = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.scr.contentSize = CGSizeMake(self.window.frame.size.width * arr.count, self.window.frame.size.height);
    self.scr.pagingEnabled = YES;
    self.scr.delegate = self;
    [self.window addSubview:self.scr];
    
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(100 * self.window.frame.size.width / 375, self.window.frame.size.height - 50, 200 * self.window.frame.size.width / 375, 40)];
    [self.window addSubview:self.page];
    self.page.numberOfPages = arr.count;
    self.page.pageIndicatorTintColor = [UIColor whiteColor];
    self.page.currentPageIndicatorTintColor = [UIColor cyanColor];
    [self.page addTarget:self action:@selector(method1:) forControlEvents:UIControlEventValueChanged];
    
    
    
    
    for (int i = 0; i < arr.count; i++) {
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(self.window.frame.size.width * i, 0, self.window.frame.size.width, self.window.frame.size.height)];
        img.image = [UIImage imageNamed:arr[i]];
        
        [self.scr addSubview:img];
    }
    [self creatrButton];
}

- (void)creatrButton
{
    self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
    _btn.frame = CGRectMake(self.window.frame.size.width * 2 + self.window.frame.size.width * 0.7, self.window.frame.size.height * 0.9, self.window.frame.size.width * 0.3, self.window.frame.size.height * 0.1);
    _btn.backgroundColor = [UIColor clearColor];
    [_btn setTitle:@"开始体验" forState:UIControlStateNormal];
    [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _btn.layer.cornerRadius = 10;
    [self.scr addSubview:_btn];
    [_btn addTarget:self action:@selector(button) forControlEvents:UIControlEventTouchUpInside];
}

- (void)button
{
    [_scr removeFromSuperview];
    [_page removeFromSuperview];
    [self createNavi];

}




-(void)method1:(UIPageControl *)page{
    [self.scr setContentOffset:CGPointMake(self.window.frame.size.width * page.currentPage, 0) animated:YES];
}


- (void)createNotificationCenter
{
    NSNotificationCenter *cent = [NSNotificationCenter defaultCenter];
    [cent addObserver:self selector:@selector(changAlpha:) name:@"ah" object:nil];
}

- (void)changAlpha:(NSNotification *)noti
{
    self.window.alpha = [noti.object floatValue];

    
}



-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    int index = self.scr.contentOffset.x / self.window.frame.size.width;
    self.page.currentPage = index;
    if (scrollView.contentOffset.x > 2 * self.window.frame.size.width + 20) {
        self.isOn = YES;
    }
}




-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (self.isOn) {
        [UIView animateWithDuration:0.1 animations:^{
            
            scrollView.alpha = 0;
        } completion:^(BOOL finished) {
            [scrollView removeFromSuperview];
            [self createNavi];
        }];
    }

}


//引导页结束


- (void)createNavi
{
    
    WXSanFangViewController *collection1 = [[[WXSanFangViewController alloc] init] autorelease];
    UINavigationController *na1 = [[[UINavigationController alloc] initWithRootViewController:collection1] autorelease];
    UIImage *image1 = [UIImage imageNamed:@"白色首页"];
    UIImage *image1_selectd = [UIImage imageNamed:@"白色首页"];
    image1 = [image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    image1_selectd = [image1_selectd imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    na1.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:image1 selectedImage:image1_selectd];
  
    
    
    
    XHEditViewController *collection2 = [[[XHEditViewController alloc] init] autorelease];
    UINavigationController *na2 = [[[UINavigationController alloc] initWithRootViewController:collection2] autorelease];
    UIImage *image2 = [UIImage imageNamed:@"白色发现"];
    UIImage *image2_selectd = [UIImage imageNamed:@"白色发现"];
    image2 = [image2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    image2_selectd = [image2_selectd imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    na2.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"发现" image:image2 selectedImage:image2_selectd];
    
    
    
    XHCollectionViewController *collection3 = [[[XHCollectionViewController alloc] init] autorelease];
    UINavigationController *na3 = [[UINavigationController alloc] initWithRootViewController:collection3];
    UIImage *image3 = [UIImage imageNamed:@"白色收藏"];
    UIImage *image3_selectd = [UIImage imageNamed:@"白色收藏"];
    image3 = [image3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    image3_selectd = [image3_selectd imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    na3.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"收藏" image:image3 selectedImage:image3_selectd];

    
    XHMoreViewController *collection4 = [[[XHMoreViewController alloc] init] autorelease];
    UINavigationController *na4 = [[[UINavigationController alloc] initWithRootViewController:collection4] autorelease];
    UIImage *image4 = [UIImage imageNamed:@"白色用户"];
    UIImage *image4_selectd = [UIImage imageNamed:@"白色用户"];
    image4 = [image4 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    image4_selectd = [image4_selectd imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    na4.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"用户" image:image4 selectedImage:image4_selectd];
    
    
    
    UITabBarController *tabBar = [[[UITabBarController alloc] init] autorelease];
    tabBar.viewControllers = @[na1, na2, na3, na4];
    
    tabBar.tabBar.translucent = NO;
    
    tabBar.delegate = self;
    
    self.window.rootViewController = tabBar;
    
    
    
   

    
    
    // 判断网络状态
    
    
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
        if ([AFStringFromNetworkReachabilityStatus(status) isEqualToString:@"Not Reachable"]) {
            
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"当前网络没有连接" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
            [alert release];
        } else if ([AFStringFromNetworkReachabilityStatus(status) isEqualToString:@"Reachable via WWAN"]){
            
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"当前网络为WLAN网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
            [alert release];
        }else if ([AFStringFromNetworkReachabilityStatus(status) isEqualToString:@"Reachable via WiFi"]){
            
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"当前网络为wifi网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
            [alert release];
        }
        
    }];
    
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
